package cn.hsw.platform.core.api;

import cn.hsw.platform.core.dto.ResultDto;
import cn.hsw.platform.core.enums.HttpEnum;
import cn.hsw.platform.core.util.ResultUtil;
import feign.hystrix.FallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * hsw-snow-flake-id 项目调用
 * @author 李子木
 * @date 2020-11-05
 */
@Component
@FeignClient(name = "hsw-manage",fallbackFactory = ManageApi.ManageApiFallbackFactory.class)
public interface ManageApi {
    /**
     * 获取权限信息
     * @param userPid 用户主键
     * @return ID
     */
    @RequestMapping(path = "/auth/user", method = RequestMethod.POST)
    ResultDto<List<Long>> authUser(@RequestParam("user_pid")Long userPid);

    /**
     * 获取角色权限信息
     * @param rolePid 用户主键
     * @return ID
     */
    @RequestMapping(path = "/auth/role", method = RequestMethod.POST)
    ResultDto<List<String>> authRole(@RequestParam("role_pid")Long rolePid);

    @Component
    class ManageApiFallbackFactory implements FallbackFactory<ManageApi> {

        @Override
        public ManageApi create(Throwable throwable) {
            return new ManageApi() {
                @Override
                public ResultDto<List<Long>> authUser(Long userPid) {
                    return ResultUtil.error(HttpEnum.MANAGE_RPC_ERROR);
                }

                @Override
                public ResultDto<List<String>> authRole(Long rolePid) {
                    return ResultUtil.error(HttpEnum.MANAGE_RPC_ERROR);
                }
            };
        }
    }
}
